package com.zbkj.service.service.project.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.common.collect.Lists;
import com.zbkj.common.enums.CommonStatusEnum;
import com.zbkj.common.model.project.EstimateInformation;
import com.zbkj.common.model.project.ProjectInfo;
import com.zbkj.common.model.system.SystemAdmin;
import com.zbkj.common.oss.OssClientUtil;
import com.zbkj.common.page.CommonPage;
import com.zbkj.common.request.project.AdminInfomationRequest;
import com.zbkj.common.request.project.InformationQueryRequest;
import com.zbkj.service.dao.project.EstimateInformationMapper;
import com.zbkj.service.service.project.EstimateInformationService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 项目估算资料表(服务端上传) 服务实现类
 * </p>
 *
 * @author 
 * @since 2024-07-08
 */
@Service
public class EstimateInformationServiceImpl extends ServiceImpl<EstimateInformationMapper, EstimateInformation> implements EstimateInformationService {

    @Resource
    private EstimateInformationMapper estimateInformationMapper;
    @Autowired
    private OssClientUtil ossClientUtil;

    @Override
    public EstimateInformation getOneRecord(Long id, Long projectId, Integer fileType, Integer status) {
        LambdaQueryWrapper<EstimateInformation> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(EstimateInformation::getDeleteFlag,0)
                .eq(EstimateInformation::getProjectId,projectId)
                .eq(EstimateInformation::getFileType,fileType);
        if(Objects.nonNull(id)) {
            queryWrapper.eq(EstimateInformation::getId,id);
        }
        if(Objects.nonNull(status)) {
            queryWrapper.eq(EstimateInformation::getStatus, status);
        }
        queryWrapper.last("limit 1");
        return this.getOne(queryWrapper,false);
    }

    @Override
    public boolean updateStatusToHis(Long projectId, List<Integer> fileTypeList) {
        if(Objects.isNull(projectId)) {
            return false;
        }
        LambdaUpdateWrapper<EstimateInformation> wrapper = new LambdaUpdateWrapper<>();
        wrapper.set(EstimateInformation::getStatus, CommonStatusEnum.HIS_STATUS.getStatus())
                .eq(EstimateInformation::getDeleteFlag, 0)
                .eq(EstimateInformation::getProjectId, projectId);
        if(CollectionUtils.isNotEmpty(fileTypeList)) {
            wrapper.in(EstimateInformation::getFileType, fileTypeList);
        }
        return this.update(wrapper);
    }

    @Override
    public boolean informationSaveProcess(AdminInfomationRequest infomationRequest, Long adminUserId) {
        //将已存在有效文件设置为历史
        this.updateStatusToHis(infomationRequest.getProjectId(), Lists.newArrayList(infomationRequest.getFileType()));
        //保存新有效文件
        EstimateInformation information = this.infomationRequestToPojo(infomationRequest, adminUserId);
        return this.save(information);
    }

    @Override
    public PageInfo<EstimateInformation> getInformationList(InformationQueryRequest queryRequest) {
        Page<EstimateInformation> pageObj = PageHelper.startPage(queryRequest.getPage(), queryRequest.getLimit());
        List<EstimateInformation> list = estimateInformationMapper.getInformationList(queryRequest);
        return CommonPage.copyPageInfo(pageObj, list);
    }

    @Override
    public int getAiInformationCount(Long projectId,Integer fileType,String reqNo) {
        return estimateInformationMapper.getAiInformationCount(projectId,fileType,reqNo);
    }

    @Override
    public EstimateInformation getValidFileObject(Long projectId, Integer fileType, String reqNo) {
        if(Objects.isNull(projectId) || Objects.isNull(fileType)) {
            return null;
        }
        LambdaQueryWrapper<EstimateInformation> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(EstimateInformation::getDeleteFlag,0)
                .eq(EstimateInformation::getProjectId,projectId)
                .eq(EstimateInformation::getFileType,fileType)
                .eq(EstimateInformation::getStatus, 0);
        if(StringUtils.isNotBlank(reqNo)) {
            queryWrapper.eq(EstimateInformation::getReqNo, reqNo);
        }
        queryWrapper.last("limit 1");
        return this.getOne(queryWrapper,false);
    }

    @Override
    public boolean delByProjectId(Long projectId, List<Integer> fileTypeList) {
        if(Objects.isNull(projectId)) {
            return false;
        }
        LambdaUpdateWrapper<EstimateInformation> wrapper = new LambdaUpdateWrapper<>();
        wrapper.set(EstimateInformation::getDeleteFlag, 1)
                .eq(EstimateInformation::getDeleteFlag, 0)
                .eq(EstimateInformation::getProjectId, projectId);
        if(CollectionUtils.isNotEmpty(fileTypeList)) {
            wrapper.in(EstimateInformation::getFileType, fileTypeList);
        }
        return this.update(wrapper);
    }


    /**
     * 资料请求对象to pojo
     * @param infRequest
     * @param adminUserId
     * @return
     */
    private EstimateInformation infomationRequestToPojo(AdminInfomationRequest infRequest, Long adminUserId) {
        EstimateInformation information = new EstimateInformation();
        information.setProjectId(infRequest.getProjectId());
        information.setFileType(infRequest.getFileType());
        information.setFileName(infRequest.getFileName());
        information.setFilePath(ossClientUtil.clearDomain(infRequest.getFilePath()));
        information.setStatus(0);
        if(Objects.nonNull(adminUserId)) {
            information.setCreateId(adminUserId);
        }
        return information;
    }
}
